/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.business;

import ch.msoftch.internal.*;
import ch.simpleel.*;
import ch.simpleel.accessors.*;

/**
 * Simple resolver assuming that a passed key is either defined manually by {@link #addELMapping(String,Class, String)} or
 * {@link #addPropertyMapping(String, Class, String)} or it is a a property existing on the bean.<br>
 * See {@link MappingResolver} as counterpart.
 * 
 * @author M. Hautle
 */
public class SimpleResolver extends StaticResolver {
    /**
     * Fetches the value of the given bean denoted by the passed property key.<br>
     * If the key is not (yet) defined then {@link #addProperties(Class)} will be called, hoping that there is a corresponding property.
     * 
     * @param <S> The type
     * @param key The property key
     * @param bean The bean from which to fetch the value
     * @return The fetched value
     * @throws IllegalArgumentException If the specified key is not defined for the given bean
     */
    @Override
    @SuppressWarnings("unchecked")
    public <S> S get(String key, Object bean, S fallback) throws IllegalArgumentException {
        final IValueAccessor accessor = getOrCreateHolder(key, bean.getClass());
        return accessor != null ? (S) accessor.getValue(bean, fallback) : fallback;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getPath(String key, Class type) {
        return getOrCreateHolder(key, type).getPath();
    }

    /**
     * Returns the expression holder for the specified key. If the key is not yet defined it will be created.
     * 
     * @param key The property key
     * @param type The type on which the key gets applied
     * @return The expression holder
     * @throws IllegalArgumentException If the specified key is not defined for the given bean
     */
    private IValueAccessor getOrCreateHolder(String key, Class type) throws IllegalArgumentException {
        final Object pk = buildKey(key, type);
        IValueAccessor p = getAccessor(pk);
        if (p != null)
            return p;
        addProperties(type);
        p = getAccessor(pk);
        if (p == null)
            throw new IllegalArgumentException(key + " is not defined for " + type.getName());
        return p;
    }

    /**
     * Adds all properties of the given class
     * 
     * @param c The class
     * @throws TechnicalException If something went wrong
     */
    protected void addProperties(Class c) throws TechnicalException {
        for (IValueAccessor n : ValueAccessorManager.getPropertyAccessors(c))
            addAccessorMapping(n.getPath(), c, n);
    }
}
